package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.utils.WriteableUtil;

public class ServiceModGenerator extends Generator{
	protected List<Domain> domains = new ArrayList<>();
	public List<Domain> getDomains() {
		return domains;
	}

	public void setDomains(List<Domain> domains) {
		this.domains = domains;
	}

	public ServiceModGenerator(){
		super();
		super.fileName = "mod.rs";
		super.standardName = "ServiceMod";
	}

	@Override
	public StatementList generateStatementList() {
		Domain sampleDomain = this.domains.get(0);
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"use std::sync::{Arc, Mutex};"));
		sList.add(new Statement(2000L,0,"use crate::Value::Null;"));
		sList.add(new Statement(3000L,0,"use crate::AppState;"));
		sList.add(new Statement(4000L,0,"use crate::Config;"));
		sList.add(new Statement(5000L,0,"use "+sampleDomain.getProjectName()+"::"+sampleDomain.getDaoimplSuffix()+"::Database;"));
		sList.add(new Statement(6000L,0,""));
		long serial = 7000L;
		for (Domain domain : this.getDomains()) {
			sList.add(new Statement(serial,0,"pub mod "+domain.getSnakeDomainName()+"_service"+";"));
			serial += 1000L;
		}
		sList.add(new Statement(serial,0,"pub async fn init_db() -> AppState<'static>{"));
		sList.add(new Statement(serial+1000L,1,"let config_file: &'static str = \"config.json\";"));
		sList.add(new Statement(serial+2000L,1,"let config = Config::from_file(config_file);"));
		sList.add(new Statement(serial+3000L,1,"let db_context = Database::new(&config.get_database_url()).await;"));
		sList.add(new Statement(serial+4000L,0,""));
		sList.add(new Statement(serial+5000L,1,"let app_state = AppState {"));
		sList.add(new Statement(serial+6000L,2,"connections: Mutex::new(0),"));
		sList.add(new Statement(serial+7000L,2,"context: Arc::new(db_context),"));
		sList.add(new Statement(serial+8000L,1,"};"));
		sList.add(new Statement(serial+9000L,1,"app_state"));
		sList.add(new Statement(serial+10000L,0,"}"));
		return WriteableUtil.merge(sList);
	}

}
